<template>

  <div class="app-container">

    <el-form ref="form" :model="form" label-width="80px" v-loading="loading">

      <el-form-item label="标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>

      <el-form-item label="分类">
        <el-cascader class="filter-item"
           v-model="form.classify_id"
           :options="mapClassifies"
           :props="{ expandTrigger: 'hover',value:'id',label:'name' }"
        ></el-cascader>
      </el-form-item>

      <el-form-item label="标签">
        <el-input v-model="form.sign" placeholder="以中文逗号分割"></el-input>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input v-model="form.key_word" placeholder="以中文逗号分割，用于SEO"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.description"></el-input>
      </el-form-item>

      <el-form-item label="封面">
        <el-upload
          class="avatar-uploader"
          :action="host+'/guard/upload?module=article'"
          accept=".png,.jpg,.jpeg"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.cover_url" :src="host+form.cover_url" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="上架/下架">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.status"
          active-text="上架"
          inactive-text="下架">
        </el-switch>
      </el-form-item>

      <el-form-item label="是否置顶">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.is_top"
          active-text="是"
          inactive-text="否">
        </el-switch>
      </el-form-item>

      <el-form-item label="正文">
        <TinymceEditor  ref="tinymceRef"  v-model="form.content">
        </TinymceEditor>
      </el-form-item>

      <!--wangEditor使用这个-->
      <!--<el-form-item label="正文">
        <div id="div1">
        </div>
      </el-form-item>-->

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
        <!-- <el-button @click="closeAddDialog" >重置</el-button> -->
      </el-form-item>



    </el-form>

  </div>


</template>

<script>
import { myGet, myPost } from '@/utils/myUtils'
import E from 'wangeditor'
import TinymceEditor from '@/components/tinymce'
const urlArticleClassifyLst = '/guard/article/classify/lst?tree=1'
const urlArticleAdd = '/guard/article/add'
export default {

  components: {
    TinymceEditor
  },
  data() {
    return {
      host:process.env.VUE_APP_HOST,
      mapClassifies:[],
      loading:false,
      //imageUrl:'',
      editor:null,
      form:{
        title:'',
        classify_id:'',
        key_word:'',
        description:'',
        cover_url:'',
        status:1,
        is_top:2,
        content:''
      },
    };
  },
  methods:{

    getArticleClassify() {
      myGet(urlArticleClassifyLst).then(res=>{
        if(res.status){
          this.mapClassifies = res.data.records
        }
      })
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          this.loading = true
          //this.form.content = this.editor.txt.html()
          myPost(urlArticleAdd,this.form).then(res => {
            this.loading = false
            if (res.status) {
              this.$message.success(res.msg)
              this.$router.push('/article/lst')
            }
          })
        }
      })
    },

    handleAvatarSuccess(res, file) {
      if(res.status){
        //this.imageUrl = res.data.records.url
        this.form.cover_url = res.data.records.filePath
      }else{
        this.$message.error('上传失败!')
      }
    },

    beforeAvatarUpload(file) {
      /*const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;*/
      return true
    },

    closeAddDialog(){
      Object.assign(this.form ,this.$options.data().form)
    },

    initEditor(){
      this.editor = new E('#div1')
      this.editor.config.uploadImgServer = this.host+'/guard/article/uploadImg'
      this.editor.config.uploadVideoServer = this.host+'/guard/article/uploadVideo'
      this.editor.create()
    }
  },

  mounted() {
    //this.initEditor()
    this.getArticleClassify()

  }
}
</script>

<style >

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
